#include "vediowidget.h"
#include "ui_vediowidget.h"
#include <iostream>
#include "opencv2/imgproc/types_c.h"

using namespace std;

vedioWidget::vedioWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::vedioWidget)
{
    ui->setupUi(this);
    VideoCap = new cv::VideoCapture;
    timer = new QTimer;
    srcShowed = new cv::Mat;
    srcCannyShowed = new cv::Mat;

    connect(this->timer,SIGNAL(timeout()),this,SLOT(UpdateImage()));
    VideoCap->open(0);
    timer->start(20);

    isStart = true;
}

vedioWidget::~vedioWidget()
{
    delete ui;
}

void vedioWidget::UpdateImage()
{
    if(this->isStart)
    {
        this->VideoCap->read(*(this->srcShowed));
        if(this->isCannyOpen)
        {
            cv::Canny(*(this->srcShowed), *(this->srcCannyShowed), 50, 150);
        }
        this->update();
    }
}

void vedioWidget::paintEvent(QPaintEvent *)
{
    if(!this->srcShowed->empty())
    {
        cv::Mat rgb;
        cv::cvtColor(*(this->srcShowed), rgb, CV_BGR2RGB);
        QImage img = QImage((const unsigned char *)(rgb.data), rgb.cols, rgb.rows, QImage::Format_RGB888);

        this->ui->videoShow->resize(img.size());
        this->ui->videoShow->setPixmap(QPixmap::fromImage(img));
        this->ui->videoShow->show();
    }
}
